﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cole.Entities;
using Cole.Service;

namespace Cole.GUI
{
    public partial class frmMatricula : Form
    {
        private IMatriculaService matriculaService;
        private ICursoBl cursoBl;
        private IAlumnoBl alumnoService;
        public frmMatricula()
        {
            InitializeComponent();
            if (matriculaService == null)
                matriculaService = new MatriculaService();

            if (alumnoService == null)
                alumnoService = new AlumnoBl();
            if (cursoBl == null)
                cursoBl = new CursoBl();

        }

        public void NuevaMatricula()
        {
            lblTitulo.Text = "Nueva Matrícula";
            matriculaDTOBindingSource.AddNew();
        }

        public void UpdatePedido(Int32 id)
        {
            lblTitulo.Text = "Editar Matrícula";
            matriculaDTOBindingSource.DataSource =
                matriculaService.GetMatriculaById(id);
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                var matricula = matriculaDTOBindingSource.Current as MatriculaDTO;

                if (matricula != null)
                {
                    matriculaService.AddMatricula(matricula);
                    matriculaDTOBindingSource.ResetBindings(true);
                    itemsBindingSource.ResetBindings(true);

                    MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            var curso = cursoBindingSource.Current as Curso;

            if (curso!=null)
            {
                var matricula = matriculaDTOBindingSource.Current as MatriculaDTO;

                var existe = matricula.Items.SingleOrDefault(p => p.IdCurso.Equals(curso.id));

                if (existe != null)
                {
                    existe.Cantidad += 1;
                }
                else
                {
                    var item = new DetalleMatriculaDTO()
                    {
                        IdCurso = curso.id,
                        Nombre =  curso.Nombre,
                        Cantidad = 1
                    };
                    matricula.Items.Add(item);
                }
                matriculaDTOBindingSource.ResetBindings(true);
                itemsBindingSource.ResetBindings(true);

                tbMatricula.SelectTab(0);
            }
        }

        private void btnQuitar_Click(object sender, EventArgs e)
        {
            var item = itemsBindingSource.Current as DetalleMatriculaDTO;

            if (item != null)
            {
                var text = string.Format("Desea quitar {0} und del curso: {1}", item.Cantidad, item.Nombre);

                var msg = MessageBox.Show(this, text, "Mensaje", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (msg == DialogResult.Yes)
                {
                    //esta forma va a funcionar siempre y cuando el id
                    //del item sea entero y autogenerado

                    if (item.Item == 0)
                    {
                        itemsBindingSource.RemoveCurrent();
                    }
                    else
                    {
                        try
                        {
                            matriculaService.QuitarDetalle(item.Item);
                            itemsBindingSource.RemoveCurrent();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }

                    var matricula = matriculaDTOBindingSource.Current as MatriculaDTO;
                    
                }
            }
        }
    }
}
